﻿using Api.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;

/**
id：帖子id，数据类型为string
postIcon：帖子图标，数据类型为int
time：发帖时间，数据类型为datetime
userId：发帖人，数据类型为int
text：内容，数据类型为string
title：标题，数据类型为string
likeNum：点赞数，数据类型为int
commentNum：评论数，数据类型为int
isVisable：是否对外可见，数据类型为bool
topicType：话题类型，数据类型为string
postType：帖子所属类型，数据类型为string
weight：rank权重，数据类型为double
**/

namespace Api.Dal
{
    public class PostDAL
    {
        public static int GetCount()
        {
            int count = 0;
            EfDemoContext ef = new EfDemoContext();
            if (ef.Posts.IsNullOrEmpty())
            {
                return 0;
            }
            count = ef.Posts.Select(p => p.id).Max();
            return count;
        }

        public static Post getPostById(int id)
        {
            EfDemoContext efDemoContext = new EfDemoContext();
            Post? post = efDemoContext.Posts.Where(p => p.id == id).FirstOrDefault();
            return post;
        }

        public static List<Post> getPost(Post post, bool isOrderByWeight)
        {
            if (isOrderByWeight == false)
            {
                // 默认order by time
                EfDemoContext efDemoContext = new EfDemoContext();
                List<Post> postList = efDemoContext.Posts.Where(p =>
                (post.id == -1 || p.id == post.id) &&
                (post.postIcon == -1 || p.postIcon == post.postIcon) &&
                (post.time == DateTime.MinValue || p.time == post.time) &&
                (post.userId == -1 || p.userId == post.userId) &&
                (post.text == "N/A" || string.Equals(p.text, post.text)) &&
                (post.title == "N/A" || string.Equals(p.title, post.title)) &&
                (post.likeNum == -1 || p.likeNum == post.likeNum) &&
                (post.commentNum == -1 || p.commentNum == post.commentNum) &&
                (post.isVisable == true || p.isVisable == post.isVisable) &&
                (post.topicType == "N/A" || string.Equals(p.topicType, post.topicType)) &&
                (post.postType == "N/A" || string.Equals(p.postType, post.postType)) &&
                (post.weight == -1 || p.weight == post.weight)
                ).OrderBy(p => p.time).ToList();
                return postList;
            }
            else
            {
                // isOrderByWeight 为 ture， order by weight从大到小
                EfDemoContext efDemoContext = new EfDemoContext();
                List<Post> postList = efDemoContext.Posts.Where(p =>
                (post.id == -1 || p.id == post.id) &&
                (post.postIcon == -1 || p.postIcon == post.postIcon) &&
                (post.time == DateTime.MinValue || p.time == post.time) &&
                (post.userId == -1 || p.userId == post.userId) &&
                (post.text == "N/A" || string.Equals(p.text, post.text)) &&
                (post.title == "N/A" || string.Equals(p.title, post.title)) &&
                (post.likeNum == -1 || p.likeNum == post.likeNum) &&
                (post.commentNum == -1 || p.commentNum == post.commentNum) &&
                (post.isVisable == true || p.isVisable == post.isVisable) &&
                (post.topicType == "N/A" || string.Equals(p.topicType, post.topicType)) &&
                (post.postType == "N/A" || string.Equals(p.postType, post.postType)) &&
                (post.weight == -1 || p.weight == post.weight)
                ).OrderByDescending(p => p.weight).ToList();
                return postList;
            }
        }






        public static int InsertPost(Post p)
        {
            if (p != null)
            {
                p.isVisable = false;
                EfDemoContext ef = new EfDemoContext();
                ef.Posts.Add(p);
                ef.SaveChanges();
                return 1;
            }
            return -2;            
        }

        public static bool DeletePostById(int postId)
        {
            Post? post = getPostById(postId);
            if (post != null)
            {
                EfDemoContext ef = new EfDemoContext();
                ef.Posts.Remove(post);
                ef.SaveChanges();
                return true;
            }
            return false;
        }

        // 删除帖子的同时，也要删除帖子id对应的评论
        public static bool DeletePostByUserId(int postUserId)
        {
            EfDemoContext efDemoContext = new EfDemoContext();
            List<Post> postListToDelete = efDemoContext.Posts.Where(p => p.userId == postUserId).ToList();
            foreach(Post post in postListToDelete)
            {
                int postIdOfCommentToDelete = post.id;
                List<Comment> commentListToDelete = efDemoContext.Comments.Where(c => c.postId == postIdOfCommentToDelete).ToList();
                foreach (Comment comment in commentListToDelete)
                {
                    efDemoContext.Comments.Remove(comment);
                    efDemoContext.SaveChanges();
                }
                efDemoContext.Posts.Remove(post);
                efDemoContext.SaveChanges();
            }
            return true;
        }

        public static bool UpdatePost(Post p)
        {
            Post? post = getPostById(p.id);
            if (post != null)
            {
                EfDemoContext ef = new EfDemoContext();
                p.postIcon = (p.postIcon != -1) ? p.postIcon : post.postIcon;
                p.time = p.time != DateTime.MinValue ? p.time : post.time;
                p.userId = (p.userId != -1) ? p.userId : post.userId;
                p.text = p.text != null ? p.text : post.text;
                p.title = p.title != null ? p.title : post.title;
                p.likeNum = p.likeNum != -1 ? p.likeNum : post.likeNum;
                p.commentNum = p.commentNum != -1 ? p.commentNum : post.commentNum;
                p.isVisable = p.isVisable != true ? p.isVisable : post.isVisable;
                p.topicType = p.topicType != null ? p.topicType : post.topicType;
                p.postType = p.postType != null ? p.postType : post.postType;
                p.weight = p.weight != -1 ? p.weight : post.weight;
                ef.Posts.Update(p);
                ef.SaveChanges();
                return true;
            }
            return false;
        }

        public static int likePost(int id)
        {
            EfDemoContext efDemoContext = new EfDemoContext();
            Post? post = getPostById(id);
            if (post == null)
            {
                throw new Exception("post is null");
                //帖子不存在，返回-1
                return -1;
            }
            post.likeNum++;
            efDemoContext.Posts.Update(post);
            efDemoContext.SaveChanges();
            return post.likeNum;


        }

        public static int dislikePost(int id)
        {
            EfDemoContext efDemoContext = new EfDemoContext();
            Post? post = getPostById(id);
            if (post == null)
            {
                throw new Exception("post is null");
                return -1;
            }
            if(post.likeNum>0)post.likeNum--;
            else
            {
                throw new Exception("likenum<=0");
                //帖子无点赞数，返回-1
                return -1;
            }
            efDemoContext.Posts.Update(post);
            efDemoContext.SaveChanges();
            return post.likeNum;
        }


        public static List<Post> getAllUnchecked()
        {

            // 默认order by time
            EfDemoContext efDemoContext = new EfDemoContext();
            List<Post> postList = efDemoContext.Posts.Where(p =>
            (p.isVisable == false)
            ).OrderBy(p => p.time).ToList();
            return postList;

        }

        public static bool pass(int id)
        {
            Post p = getPostById(id);
            if (p == null) return false;
            EfDemoContext ef = new EfDemoContext();
            p.isVisable = true;
            ef.Posts.Update(p);
            ef.SaveChanges();
            return true;
        }


    }
}
